package com.company.jrlz.web.screens.assembleworkorder;

import com.company.jrlz.entity.ProcessWorkOrder;
import com.company.jrlz.service.AssembleWorkOrderService;
import com.company.jrlz.service.ProcessWorkOrderService;
import com.haulmont.cuba.core.entity.FileDescriptor;
import com.haulmont.cuba.core.global.DataManager;
import com.haulmont.cuba.core.global.FileStorageException;
import com.haulmont.cuba.gui.Dialogs;
import com.haulmont.cuba.gui.Notifications;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.model.CollectionContainer;
import com.haulmont.cuba.gui.model.CollectionLoader;
import com.haulmont.cuba.gui.screen.*;
import com.company.jrlz.entity.AssembleWorkOrder;
import com.haulmont.cuba.gui.screen.LookupComponent;
import com.haulmont.cuba.gui.upload.FileUploadingAPI;

import javax.inject.Inject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@UiController("jrlz_AssembleWorkOrder.browse")
@UiDescriptor("assemble-work-order-browse.xml")
@LookupComponent("assembleWorkOrdersTable")
@LoadDataBeforeShow
public class AssembleWorkOrderBrowse extends StandardLookup<AssembleWorkOrder> {
    @Inject
    private Dialogs dialogs;


    @Inject
    private Label<String> fiftyOrderTime;
    @Inject
    private Label<String> awaitSendOrder;
    @Inject
    private Notifications notifications;
    @Inject
    private Label<String> awaitExecuteOrder;

    @Inject
    private DataGrid<AssembleWorkOrder> assembleWorkOrdersTable;

    @Inject
    private FileUploadField uploadField;

    @Inject
    private FileUploadingAPI fileUploadingAPI;
    private FileDescriptor fd;

    @Inject
    protected DataManager dataManager;

    @Inject
    AssembleWorkOrderService assembleWorkOrderService;
    @Inject
    private CollectionLoader<AssembleWorkOrder> assembleWorkOrdersDl;
    @Inject
    private CollectionContainer<AssembleWorkOrder> assembleWorkOrdersDc;

    @Subscribe("uploadField")
    public void onUploadFieldFileUploadSucceed(FileUploadField.FileUploadSucceedEvent event) throws IOException {
        File file = fileUploadingAPI.getFile(uploadField.getFileId());
        if (file != null) {
//            notifications.create()
//                    .withCaption("File is uploaded to temporary storage at " + file.getAbsolutePath())
//                    .show();
        }

        FileDescriptor fd = uploadField.getFileDescriptor();
        try {
            fileUploadingAPI.putFileIntoStorage(uploadField.getFileId(), fd);
        } catch (FileStorageException e) {
            throw new RuntimeException("Error saving file to FileStorage", e);
        }
        dataManager.commit(fd);
//        notifications.create()
//                .withCaption("Uploaded file: " + uploadField.getFileName())
//                .show();
        String a= assembleWorkOrderService.importData(file,fd,uploadField.getFileName());
        notifications.create()
                .withCaption(a)
                .show();
        assembleWorkOrdersDl.load();

    }

    @Subscribe("uploadField")
    public void onUploadFieldFileUploadError(UploadField.FileUploadErrorEvent event) {
        notifications.create()
                .withCaption("File upload error")
                .show();
    }

    //工单执行节拍点击赋值
    @Subscribe("popupButton")
    public void onPopupButtonPopupVisibility(PopupButton.PopupVisibilityEvent event) {
        awaitExecuteOrder.setValue("待执行工单数："+100);
        awaitSendOrder.setValue("待下发工单数："+80);
        fiftyOrderTime.setValue("近50个工单平均执行时间："+10+"分钟");
    }

    public void sendPlc(){
        List<AssembleWorkOrder> selectItems = new ArrayList<>();
        Set<AssembleWorkOrder> items = assembleWorkOrdersTable.getSelected();
        selectItems.addAll(items);
        if(selectItems.isEmpty()){
            notifications.create().withCaption("请最少选择一条数据").show();
        }else {
            notifications.create().withCaption("批量下发PLC成功").show();
        }
    }

    public void dialog(){
//        popupView.setPopupVisible(true);
        dialogValue("1","原料库备料不充足，缺少一个外缸，2个活柱，是否继续下发");
//        dialogValue("2","产线机床加工参数和当前计划工单物料类型不匹配，请调节机床参数发");
    }
    //提醒弹框
    public void dialogValue(String type,String value){
        dialogs.createOptionDialog()
                .withCaption("批量下发PLC")
                .withMessage(value)
                .withType(Dialogs.MessageType.CONFIRMATION)
                .withActions(
                        new DialogAction(DialogAction.Type.OK)//确定逻辑处理
                                .withHandler(e ->
                                        dialogHandle(type)
                                ),

                        new DialogAction(DialogAction.Type.CANCEL))
                .show();
    }
    //弹框确认处理逻辑
    public void dialogHandle(String type){
        if(type.equals("1")){
            notifications.create()
                    .withCaption("OK1")
                    .show();
        }else if(type.equals("2")){
            notifications.create()
                    .withCaption("OK2")
                    .show();
        }

    }
}
